
package com.company.project.controller;

import com.alibaba.fastjson.JSONObject;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.service.UserService;
import com.company.project.utils.XmlTool;
import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.dom4j.DocumentException;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * webservice 请求实例
 *
 * @author PengChenXi
 * @since 2022/05/18.
 */
@RestController
@RequestMapping("/es")
@Slf4j
public class EsController {
    @Resource
    private UserService userService;
    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient esClient;

    /**
     * 批量导入es
     *
     * @return
     */
    @PostMapping("/createBulkDocument")
    public Result createBulkDocument() throws IOException {
        long startTime = System.currentTimeMillis();
//        //1、构建 创建索引的请求
//        CreateIndexRequest request = new CreateIndexRequest("hf_doc_index");//索引名
//        //2、客户端执行请求,获取响应
//        CreateIndexResponse response = esClient.indices().create(request, RequestOptions.DEFAULT);
//        //3、打印
//        System.out.println("创建成功，创建的索引名为：" + response.index());

        try {
            //构建批量插入的请求
            BulkRequest request1 = new BulkRequest();
            //设置超时时间
            request1.timeout("10s");

            //设置数据
            List<Map<String, Object>> list = userService.getHfDocIndexList();

            //批量插入请求设置
            for (int i = 0; i < list.size(); i++) {
                request1.add(
                        new IndexRequest("hf_doc_index")//设置索引
                                .id(String.valueOf(i + 1))//设置文档的id，如果没有指定，会随机生成，自己测试
                                .source(com.alibaba.fastjson.JSONObject.toJSONString(list.get(i)), XContentType.JSON)//设置要添加的资源，类型为JSON
                );
            }
            BulkResponse response1 = esClient.bulk(request1, RequestOptions.DEFAULT);
            System.out.println("批量插入是否失败：" + response1.hasFailures());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        log.error("运行时间 : {} ms", System.currentTimeMillis() - startTime);
        return ResultGenerator.genSuccessResult();
    }


}
